package com.wrupple.muba.base.client.module;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.shared.EventBus;
import com.wrupple.muba.common.shared.ServiceBus;
import com.wrupple.muba.common.shared.State.Context;
import com.wrupple.muba.common.shared.StateTransition;

public class MubaServiceBus implements ServiceBus {
	
	
	Map<String, CommandService> serviceRegistry;
	
	public MubaServiceBus() {
		super();
		serviceRegistry = new HashMap<String, ServiceBus.CommandService>();
	}

	@Override
	public void add(String id, CommandService service) {
		serviceRegistry.put(id, service);		
	}

	@Override
	public CommandService get(String id) {
		return serviceRegistry.get(id);
	}

	@Override
	public void excecuteCommand(String command, JavaScriptObject properties,
			EventBus eventBus, Context processContext, StateTransition<? extends JavaScriptObject> callback) {
		CommandService service = get(command);
		service.prepare(properties,eventBus,processContext, callback);
		Scheduler.get().scheduleDeferred(service);
	}


}
